function isGetPageRoutes(route = '') {
	return new Promise((rel, rej) => {
		let routPages = getCurrentPages()[getCurrentPages().length - 1]
		console.log(!(routPages.route === route))
		return rel(routPages.route === route)
	})

}

/*权限管理  */
function scopeAlbum() {
	// #ifdef MP-KUAISHOU || MP-WEIXIN
	let auth = 'scope.writePhotosAlbum'
	// #endif
	// #ifdef MP-TOUTIAO
	let auth = 'scope.album'
	// #endif
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success(res) {
				console.log(res, 'sssss')
				if (res.authSetting[auth] == false) {
					uni.showModal({
						title: '温馨提示',
						content: '为了更好的体验小程序,请授权相册哦~',
						showCancel: false,
						success() {
							uni.openSetting({
								success(res2) {
									if (res2.authSetting[auth]) {
										resolve();
									} else {
										reject('error2');
									}
								},
								fail(err) {
									reject('error2');
								}
							})
						}
					})
				} else if (res.authSetting['scope.camera'] == false) {
					uni.showModal({
						title: '温馨提示',
						content: '为了更好的体验小程序,请授权相机哦~',
						showCancel: false,
						success() {
							uni.openSetting({
								success(res2) {
									if (res2.authSetting['scope.camera']) {
										resolve();
									} else {
										reject('error2');
									}
								},
								fail(err) {
									reject('error2');
								}
							})
						}
					})
				} else {
					resolve();
				}
			},
			fail(err) {
				reject('error');
			}
		})
	}).catch(e => {
		console.log(e)
	});
}

function jumpUrl(url) {
	console.log(url,'===>url')
	let pages = getCurrentPages();
	if (pages.length > 1 && url.indexOf(pages[pages.length - 1].route) >= 0) {
		uni.redirectTo({
			url: url
		})
	} else {
		uni.navigateTo({
			url: url
		})
	}
}

function formatMillisecondsToTime(differenceInMilliseconds) {
	// 转换为秒
	var differenceInSeconds = Math.floor(differenceInMilliseconds / 1000);

	// 创建一个表示秒数的对象
	var timeObject = {
		"hours": Math.floor(differenceInSeconds / 3600),
		"minutes": Math.floor((differenceInSeconds % 3600) / 60),
		"seconds": differenceInSeconds % 60
	};

	// 补零并格式化
	function pad(num, size) {
		return ('00' + num).slice(size * -1);
	}
	return pad(timeObject.hours, 2) + ':' + pad(timeObject.minutes, 2) + ':' + pad(timeObject.seconds, 2);
}

function dateFormat(dateTime = null, fmt = 'yyyy-mm-dd') {
	// 如果为null,则格式化当前时间
	if (!dateTime) dateTime = Number(new Date());
	// 如果dateTime长度为10或者13，则为秒和毫秒的时间戳，如果超过13位，则为其他的时间格式
	if (dateTime.toString().length == 10) dateTime *= 1000;
	let date = new Date(dateTime);
	let ret;
	let opt = {
		"y+": date.getFullYear().toString(), // 年
		"m+": (date.getMonth() + 1).toString(), // 月
		"d+": date.getDate().toString(), // 日
		"h+": date.getHours().toString(), // 时
		"M+": date.getMinutes().toString(), // 分
		"s+": date.getSeconds().toString() // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (let k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}

function digitToChinese(digit) {
	var digitMap = {
		'0': '零',
		'1': '一',
		'2': '二',
		'3': '三',
		'4': '四',
		'5': '五',
		'6': '六',
		'7': '七',
		'8': '八',
		'9': '九'
	};
	return digitMap[digit];
}

function postShareHttp(contentItemId) {
	console.log(contentItemId, '===>contentItemId')
	this.$http.post(`/yaolexue-service-portal/contentItem/shareContentItem/${contentItemId}`, {

	}).then(res => {
		if (res.code == 200) {
			uni.showToast({
				title: '分享成功'
			})
		}
	})
}

function base64ImgToFilePath(base64Url, filePath = null) {
	return new Promise((rel,rej) => {
		let base64 = base64Url.replace(/^data:image\/\w+;base64,/, ""); //去掉data:image/png;base64,
		let path = filePath ? filePath : wx.env.USER_DATA_PATH + `/yaoxue${this.$u.guid(8)}_qrcode.png`;
		console.log(path,'====>path')
		console.log(base64)
		uni.getFileSystemManager().writeFile({
			filePath: path, //创建一个临时文件名
			data: base64, //写入的文本或二进制数据
			encoding: 'base64', //写入当前文件的字符编码
			success: res => {
				console.log(path,'===>转')
				return rel(path)
			},
			fail: err => {
				console.log(err)
				return rej('转换失败')
			}
		})
	})
	
}

module.exports = {
	isGetPageRoutes,
	scopeAlbum,
	jumpUrl,
	formatMillisecondsToTime,
	dateFormat,
	postShareHttp,
	base64ImgToFilePath
}